package com.tlias.controller;



import com.tlias.jwt.annotation.LoginToken;
import com.tlias.jwt.service.TokenService;
import com.tlias.pojo.Emp;
import com.tlias.pojo.PageBean;
import com.tlias.pojo.Result;
import com.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;


@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    EmpService empService;
    @Autowired
    TokenService tokenService;



    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @LoginToken
    @PutMapping
    public Result UpdateEmp( @RequestBody Emp emp) {
       log.info("修改员工 -> 请求参数：emp[{}]",emp);
        Integer rs = empService.updateEmp(emp);
        if (rs == 1){
            return Result.success();
        }
        return Result.error("修改失败");
    }


    /**
     * 根据id查员工
     *
     * @param id
     * @return
     */
    @LoginToken
    @GetMapping("/{id}")
    public Result getEmpInfoById(@PathVariable Integer id) {
        Result result = empService.findEmpById(id);
        return result;
    }

    /**
     * 增加员工
     *
     * @param emp
     * @return
     */
    @LoginToken
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("增加员工请求参数 -> emp:[{}]",emp);
        Integer rs = empService.addEmp(emp);
        if (rs == 1){
            return Result.success();
        }else {
            return Result.error("新增失败");
        }

    }


    /**
     * 删除员工
     *
     * @param ids
     * @return
     */
    @LoginToken
    @DeleteMapping("/{ids}")
    public Result delEmp(@PathVariable Integer[] ids) {
        Integer rs = empService.deleteEmp(ids);
        if (rs >0){
            return Result.success();
        }
        return Result.error("删除失败");
    }


    /**
     *  分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @LoginToken
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer gender,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志
        log.info("分页查询，参数：page[{}],pageSize[{}],name[{}],gender[{}],begin[{}],end[{}]", page, pageSize,name,gender,begin,end);

          PageBean  pageBean = empService.page(page, pageSize,name,gender,begin,end);

        return Result.success(pageBean);
    }


}
